import java.util.Stack;

class Solution {
    public boolean isValid(String s) {
        if(s == null){
            return false;
        }
        //创一个character栈 解决问题
        Stack<Character> stack = new Stack<>();

        for (int i = 0; i < s.length(); i++) {
            char ch = s.charAt(i);
            //若是左括号就存
            if(ch == '{' || ch== '(' || ch == '['){
                stack.push(ch);
            }else {
                if(stack.empty()){
                    //右括号 之前没遇到左括号
                    return false;
                }
                //右括号 之前 有左括号，匹配
                char ch2 = stack.peek();
                if((ch2 == '[' && ch == ']') || (ch2 == '(' && ch == ')') || (ch2 == '{' && ch == '}')) {
                    stack.pop();
                }else {
                    return false;
                }
            }
        }

        //还剩左括号没匹配
        if(!stack.empty()) {
            return false;
        }
        return true;
    }
}